package qnb.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import org.springframework.web.servlet.ModelAndView;
import qnb._common._ModuleKey;
import qnb._common.easyui.EasyuiGridData;
import qnb._common.easyui.EasyuiPageVO;
import qnb._common.tools.StringTool;
import qnb._common.vo.MessageVOFactory;
import qnb.controller.vo.SubjectWebVO;
import qnb.entity.Subject;
import qnb.service.SubjectService;
import weixinkeji.vip.expand.poi.JWEOfficeWebTool;
import weixinkeji.vip.jweb.power.ann.JWPIdentifiter;

/**
 * 
 * @author wangchunzi
 *
 */
@Controller
@RequestMapping("/subject")
public class SubjectController {
	@Autowired
	private SubjectService service;

	@JWPIdentifiter("subject:add")
	// 跳转到添加视图（jsp页面）
	@RequestMapping("/forward/subjectAdd")
	public ModelAndView forwardTAdd(ModelAndView modelAndView) {
		modelAndView.setViewName("subject/subjectAdd");
		return modelAndView;
	}

	//@JWPIdentifiter("subject:add2")
	// 跳转到添加视图（jsp页面）
	@RequestMapping("/forward/subjectAdd2")
	public String forwardTAdd2() {
		return "subject/subjectAdd2";
	}

	// 跳转到查询视图（jsp页面）
	@JWPIdentifiter("subject:select")
	@RequestMapping("/forward/subjectList")
	public ModelAndView forwardList(ModelAndView modelAndView) {
		modelAndView.setViewName("subject/subjectList");
		return modelAndView;
	}

//=============================================================================

	// 添加
	@JWPIdentifiter("subject:add")
	@RequestMapping("/subjectAdd")
	@ResponseBody
	public String add(Subject obj) {
		return MessageVOFactory.getAddMessage(service.insert(obj));
	}

	// 添加2
	@RequestMapping("/subjectAdd2")
	@ResponseBody
	public String add2(SubjectWebVO vo) {
		//从表单分离出：表体数据 。假设有对象属性 head1、head2
		System.out.println(vo.getHead1()+"//"+vo.getHead2());
		System.err.println(vo.getBodyData());
		//从表单分离出：表体数据
		List<Subject> list= StringTool.jsonStringToList(Subject.class, vo.getBodyData());
		for(Subject obj:list) {
			System.out.println(obj.getSubjectInfo()+"//"+obj.getSubjectName());
		}
		return MessageVOFactory.getAddMessage(1);
	}

	// 删除
	@JWPIdentifiter("subject:dell")
	@RequestMapping("/subjectDell")
	@ResponseBody
	public String dell(Integer id) {
		if (null == id || id < 0) {
			return null;
		}
		return MessageVOFactory.getDellMessage(service.delete(id));
	}

	// 修改
	@JWPIdentifiter("subject:update")
	@RequestMapping("/subjectUpdate")
	@ResponseBody
	public String update(Subject obj) {
		return MessageVOFactory.getUpdateMessage(service.update(obj));
	}

	// 查询一个科目对象
	// 目前 结合修改使用
	@RequestMapping("/subjectOne")
	@ResponseBody
	public Object selectOne(Integer id) {
		if (null == id || id < 0) {
			return null;
		}
		return service.selectOne(id);
	}

	// 分页查询
	@RequestMapping("/subjectList")
	@ResponseBody
	public Object list(EasyuiPageVO vo) {
		if (vo.isSqlError()) {
			System.out.println("======================");
			return null;
		}

		String whereSql = vo.getWhereSql(_ModuleKey.Subject);
		return EasyuiGridData.getEasyuiGridData(service.count(whereSql),
				service.select(whereSql, vo.getOrderbySQL(), vo.getPageSQL(), vo.getRows()));
	}

	// 分页查询
	@JWPIdentifiter("subject:down")
	@RequestMapping("/subjectList/down")
	public void dow(HttpServletResponse resp ,EasyuiPageVO vo) throws ServletException, IOException {
		if (vo.isSqlError()) {
			System.out.println("======================");
			return;
		}
		List<Subject> list=service.selectToDown(vo.getWhereSql(_ModuleKey.Subject));
		
		JWEOfficeWebTool.downloadExcelFile(resp, list, "科目明细.xls", "科目", true);
	}
	
}
